package com.zh.api.controller.goods;


import com.alibaba.fastjson.JSONObject;
import com.zh.common.base.BaseController;
import com.zh.common.utils.PageUtils;
import com.zh.common.utils.Query;
import com.zh.common.utils.ResultVO;
import com.zh.goods.domain.GoodsDO;
import com.zh.goods.domain.GoodsSublistDO;
import com.zh.goods.service.GoodsRecordService;
import com.zh.goods.service.GoodsService;
import com.zh.goods.service.GoodsSublistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-12-12 09:50:30
 */
 
@Controller
@RequestMapping("/goods")
@Api("商品管理")
public class GoodsController extends BaseController {
	@Autowired
	private GoodsService scGoodsService;
	@Autowired
	private GoodsRecordService goodsRecordService;
	@Autowired
	private GoodsSublistService scGoodsSublistService;

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取商品列表")
	public PageUtils list(@RequestBody Map<String, Object> params){
		params.put("servicerId",getCompanyUser().getCompanyId());
		String  s = (String) params.get("s");
		String[] idAll=s.split(",");
		params.put("ss",idAll);
		//查询列表数据
        Query query = new Query(params);
		List<GoodsDO> scGoodsList = scGoodsService.list(query);
		int total = scGoodsService.count(query);
		PageUtils pageUtils = new PageUtils(scGoodsList, total);
		return pageUtils;
	}

	@RequestMapping(value = "getGoodsById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("获取商品详情")
	ResultVO getGoodsById(@RequestBody GoodsDO scGoodsDo){
		GoodsDO scGoods = scGoodsService.get(scGoodsDo.getId());
	    return ResultVO.success(scGoods);
	}

	/**
	 * @Author: lds
	 * @Description: 上架
	 * @Date: Created in 17:14 2019/5/27 0027
	 * @param [id]
	 * @return com.zh.common.utils.ResultVO
	 */
	@RequestMapping(value = "shelf", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("商品上/下架")
	ResultVO shelf(@RequestBody GoodsDO scGoodsDo){
		GoodsDO scGoods = scGoodsService.get(scGoodsDo.getId());
		scGoods.setFlag(scGoodsDo.getFlag());
		scGoodsService.update(scGoods);
		return ResultVO.success();
	}


	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("保存商品")
	public ResultVO save(@RequestBody GoodsDO scGoods){
		Map<String, Object> map = new HashMap<>();
		if(scGoodsService.save(scGoods)>0){
			map.put("gid",scGoods.getId());
			return ResultVO.success(map);
		}
		return ResultVO.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("修改商品")
	public ResultVO update(@RequestBody GoodsDO scGoods){
		scGoodsService.update(scGoods);
		return ResultVO.success();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("删除商品")
	public ResultVO remove(@RequestBody GoodsDO scGoods){
		if(scGoodsService.removeGoodsAndSubList(scGoods.getId())>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value = "batchRemove", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除商品")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		String[] ids= (String[]) jsonParam.get("ids");
		if(scGoodsService.batchRemoveGoodsAndSubList(ids)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	/**
	 * 查询所有规格和颜色
	 */
	@ResponseBody
	@RequestMapping(value = "getDataByGoodsId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取商品所有规格和颜色")
	public ResultVO  getDataByGoodsId(@RequestBody JSONObject jsonParam){
		String goodsId=jsonParam.getString("goodsId");
		List<GoodsSublistDO>  list=scGoodsSublistService.getDataByGoodsId(goodsId);
		Map map=new HashMap();
		map.put("list",list);
		return ResultVO.success(map);
	}


	/**
	 * 进货
	 * @param goodsDO
	 * @return
     */
	@RequestMapping(value = "stockGoods", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("商品进货")
	public ResultVO stockGoods(@RequestBody GoodsDO goodsDO) {
		if(scGoodsService.updateAndSaveRecord(goodsDO)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	/**
	 * 获取所有商品
	 * @return
     */
	@ResponseBody
	@RequestMapping(value = "type", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation("获取所有商品")
	public ResultVO fun(){
		List<GoodsDO> all = scGoodsService.getAll();
		return ResultVO.success(all);
	}
}
